/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.report;

import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import database.IssuingDAO;
import database.StaffAuthenticationDAO;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import model.Issue;
import model.Staff;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Trang
 */

@Namespace("/Administration/Revenue")
public class WRevenueBetweenDate extends ActionSupport{
    private String startDate;
    private String endDate;
    private List<Issue> issues;
    private String date;
    private String title;
    private double totalAmount;
    
    
    public IssuingDAO issueDAO = new IssuingDAO();

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public double getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(double totalAmount) {
        this.totalAmount = totalAmount;
    }
    
    
    public List<Issue> getIssues() {
        return issues;
    }

    public void setIssues(List<Issue> issues) {
        this.issues = issues;
    }

    public String getStartDate() {
        return startDate;
    }

    @RequiredStringValidator(message = "Start date is required.")
    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public String getEndDate() {
        return endDate;
    }

    @RequiredStringValidator(message = "End date is required.")
    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }
    @Action(value = "revenuebetweendate", results = {
        @Result(name = "success", location = "/manageReport/w_displayRevenuebydate.jsp")
    })
    public String RevenueBetween() throws ParseException{
        
        Map session = ActionContext.getContext().getSession();
        Staff staff = (Staff) session.get("staff");
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        Date start = dateFormat.parse(startDate);
        Date end = dateFormat.parse(endDate);

        issues = issueDAO.getIssueBetweenDate(start, end);
        
        date = startDate+" - "+endDate;
        title = "Total Revenue by product ";
        totalAmount = 0;
        for(Issue item: issues)
        {
            totalAmount += item.getOrder().getTotalAmount();
        }
        new StaffAuthenticationDAO().addAction("Create Product Revenue Report", staff.getIdStaff());
        
        return SUCCESS;
    }
}
